package cn.jhz.learn.blog.controller.core.open;

import cn.jhz.learn.blog.common.bean.JsonData;
import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.service.core.open.article.ArticleService;
import cn.jhz.learn.blog.service.core.open.article.ArticleSubfieldService;
import cn.jhz.learn.blog.util.JwtUtil;
import cn.jhz.learn.blog.vo.SubfieldPageView;
import cn.jhz.learn.blog.vo.article.query.page.ArticlePageView;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jose4j.jwt.MalformedClaimException;
import org.jose4j.jwt.consumer.InvalidJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/public/core/article/")
public class ArticleController {
    private final ArticleService articleService;
    private final ArticleSubfieldService subfieldService;

    @Autowired
    public ArticleController(ArticleService articleService, ArticleSubfieldService subfieldService) {
        this.articleService = articleService;
        this.subfieldService = subfieldService;
    }

    @GetMapping("list")
    public JsonData getPage(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER, required = false)String jwt,
                            @NotNull @Validated ArticlePageView pageView) throws InvalidJwtException, MalformedClaimException {

        if(StringUtils.isNotBlank(jwt))
            return JsonData.success(articleService.getPage(JwtUtil.getInstance().getUid(jwt), pageView));
        else
            return JsonData.success(articleService.getPage(pageView));
    }

    @GetMapping("detail/{article_id}")
    public JsonData getDetail(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER, required = false)String jwt,
                                         @PathVariable("article_id")Integer articleId) throws InvalidJwtException,
            MalformedClaimException {
        if(StringUtils.isNotBlank(jwt))
            return JsonData.success(articleService.getDetail(JwtUtil.getInstance().getUid(jwt), articleId));
        else
            return JsonData.success(articleService.getDetail(articleId));
    }

    @GetMapping("subfield/list")
    public JsonData getSubfields(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER, required = false)String jwt,
                                @NotNull @Validated SubfieldPageView pageView) throws InvalidJwtException, MalformedClaimException {

        if(StringUtils.isNotBlank(jwt))
            return JsonData.success(subfieldService.getPage(JwtUtil.getInstance().getUid(jwt), pageView));
        else
            return JsonData.success(subfieldService.getPage(pageView));

    }

    @GetMapping("subfield/{subfield_id}")
    public JsonData getSubfield(@RequestHeader(value = GLOBAL.TOKEN_INFO.AUTH_HEADER, required = false)String jwt,
                                     @PathVariable("subfield_id") Integer subfieldId) throws InvalidJwtException, MalformedClaimException {

        if(StringUtils.isNotBlank(jwt))
            return JsonData.success(subfieldService.getDetail(JwtUtil.getInstance().getUid(jwt), subfieldId));
        else
            return JsonData.success(subfieldService.getDetail(subfieldId));

    }
}
